package com.earthquake.monitor_system.controller;

import com.earthquake.monitor_system.controller.resultType.IconType;
import com.earthquake.monitor_system.controller.resultType.userMenuType;
import com.earthquake.monitor_system.domain.SysMenuDomain;
import com.earthquake.monitor_system.domain.UserMenuDomain;
import com.earthquake.monitor_system.service.impl.SysMenuImp;
import com.earthquake.monitor_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class SysMenuController {
    @Autowired
    private SysMenuImp sysMenuImp;
    @GetMapping("/getMenuList")
    public  Result getMenuList(){
        List<SysMenuDomain> menuList = sysMenuImp.getMenuList();
        return Result.success("获取数据成功",menuList);
    }
    @GetMapping("/getUserMenuList")
    public  Result getUserMenuList(@RequestParam Integer id){
        List<SysMenuDomain> menuList = sysMenuImp.getUserMenuList(id);
        return Result.success("获取数据成功",menuList);
    }
    @GetMapping("/getIcon")
    private Result getIconList() {
        List<IconType> iconList = sysMenuImp.getIcon();
        return Result.success("获取icon成功！",iconList);
    }
    @PostMapping("/insetParentMenu")
    private Result insetParentMenu(@RequestBody SysMenuDomain sysMenuDomain) {
        HashMap<String,Object> map = sysMenuImp.insetParentMenu(sysMenuDomain);
        Object isExist = map.get("isExist");
        if (isExist.equals(true)) {
            return Result.success(map.get("msg").toString(),false);
        }
        if(map.get("isInsert").equals(false)){
            return Result.success("添加菜单失败！",false);
        }
       return Result.success("添加菜单成功！",true);
    }
    @PostMapping("/insetChildrenMenu")
    private Result insetChildrenMenu(@RequestBody SysMenuDomain sysMenuDomain) {
        HashMap<String,Object> map = sysMenuImp.insetChildrenMenu(sysMenuDomain);
        Object isExist = map.get("isExist");
        if (isExist.equals(true)) {
            return Result.success(map.get("msg").toString(),false);
        }
        if(map.get("isInsert").equals(false)){
            return Result.success("添加菜单失败！",false);
        }
        return Result.success("添加菜单成功！",true);
    }
    @PostMapping("/updateMenu")
    public  Result  updateMenu(@RequestBody SysMenuDomain sysMenuDomain){
        Boolean aBoolean = sysMenuImp.updateMenu(sysMenuDomain);
        if(!aBoolean) {
            return Result.success("更新失败！",false);
        }
        return Result.success("更新成功！",true);
    }
    @GetMapping("/DeleteMenu")
    public Result DeleteMenu(@RequestParam Integer id) {
        Boolean aBoolean = sysMenuImp.DeleteMenu(id);
        if(!aBoolean) {
            return Result.success("删除失败！",false);
        }
        return Result.success("删除成功！",true);

    }
    @PostMapping("/userMenu")
    public Result userMenu(@RequestBody userMenuType userMenu){
        Boolean aBoolean = sysMenuImp.userMenu(userMenu.getId(), userMenu.getMenuList());
        if(!aBoolean) {
            return Result.success("更新失败！",false);
        }
        return Result.success("更新成功！",true);
    }
    @GetMapping("/queryUserMenu")
    public Result queryUserMenu(@RequestParam Integer userId){
        List<Integer> userMenu = sysMenuImp.getUserMenu(userId);
        return Result.success("获取成功！",userMenu);
    }
}
